# -*- coding: utf-8 -*-
#   /**
#   * Copyright (c) 2022 Beijing Jiaotong University
#   * PhotLab is licensed under [Open Source License].
#   * You can use this software according to the terms and conditions of the [Open Source License].
#   * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
#   *
#   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
#   * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
#   * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#   *
#   * See the [Open Source License] for more details.
#   */
#   /**
#   * Author: Zheng Wang
#   * Created: Feb. 11, 2023
#   * Supported by: National Key Research and Development Program of China
#   */

import phot_engine as pe
from engine_test.test_data.fetch_data_util import util
import time

SIMULATION_PROJECT_PATH = r"../test_data/GDUT_20240214_blackbox.phtg"

if __name__ == '__main__':

    vertex_data_list, edge_data_list, global_data_list = util.export_data_from_phtg(
        phtg_filepath=SIMULATION_PROJECT_PATH)

    # generate graph entity
    ge = pe.GraphEntity(scrutiny=True)
    # add vertex,edge,global parameter to graph entity
    for vertex_data in vertex_data_list:
        ge.add_vertex(vertex_data)
    for edge_data in edge_data_list:
        ge.add_edge(edge_data)
    ge.set_system_global_parameter(global_data_list)

    # generate simulation task
    simulation_task = ge.gen_simulation_task(task_id="test_project", priority=pe.TaskPriority.LOW, execution_time=None)

    simulation_task.show_computational_graph()
    # generate computing engine
    engine = pe.PhotLabEngine()
    engine.add_task(simulation_task.get_task_id(), simulation_task)
    print("Simulation Task Status: ", engine.get_task_status("test_project"))
    print("\033[0;32; mEngine starts calculation!\033[0m")
    s_time = time.time()
    engine.calculate_task(simulation_task.get_task_id())
    e_time = time.time()
    print("\033[0;31; mEngine finished calculation!\033[0m")
    vertex_id = "gen_bits_1"
    vertex_output_signal = simulation_task.get_signal_by_vertex_id(vertex_id)
    print("{}'s output signal: ".format(vertex_id))
    for elem in vertex_output_signal:
        print("destination_vertex_id:{},destination_vertex_port_id:{},signal:{}".format(elem["destination_vertex_id"],
                                                                                        elem[
                                                                                            "destination_vertex_port_id"],
                                                                                        elem["signal"]))
    vertex_id_2 = "bits_error_count_1"
    vertex_output_signal = simulation_task.get_signal_by_vertex_id(vertex_id_2)
    print("{}'s output signal: ".format(vertex_id_2))
    for elem in vertex_output_signal:
        print("destination_vertex_id:{},destination_vertex_port_id:{},signal:{}".format(elem["destination_vertex_id"],
                                                                                        elem["destination_vertex_port_id"],
                                                                                        elem["signal"]))

    # analyzer_edge_list = simulation_task.get_all_analyzer_output_signal_list()
    # for signal in analyzer_edge_list:
    #     print(signal)